/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ", "ajaxServ","datagridServ","treegridServ","bizProto","btnServ"],
		function($, msgServ, ajaxServ,datagridServ,treegridServ,bizProto,btnServ) {

			'use strict';

			var _bizProto,righttreeExpanded=false,_buttonConfig,
				$activityList,$activityListGrid,$flowList,$flowListGrid,
				$edit_window ,
				baseLeftQueryUrl,
				baseRightByLeftIdQueryUrl,
				flowNodeUrl,
				editIndex=-1,
				procDefinitionId
				
				

			var private_method = {
				
				//初始化方法
				init : function(doc) {
					if (window.console) {
					console.info('flowTaskConfig modules gets ready and been loaded.');
					}
					_bizProto = new bizProto({doc:doc,rootId:'panel-flowTaskConfig',panelSelector:'div.well',subSystemCode:'workflow',moduleCode:'flowTaskConfig'});
					_bizProto.gridHeight -= 15;
					
					_buttonConfig={
						
						 "保存":private_method.save,
						 "新增":private_method.add,
						 "删除":private_method.remove,
						 "刷新缓存":private_method.reload
						 							
					}
					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
				},
				
				//初始化路径
				initPath:function(){					
					baseLeftQueryUrl = _bizProto.rootPath +
						'/flowDefinition/getLastVersionListDomain.action';
					
					
					baseRightByLeftIdQueryUrl = _bizProto.rootPath +
						'/flowTaskConfig/getByDefinitionIdListDomain.action';
						
					flowNodeUrl =_bizProto.rootPath +'/flowDefinition/getTaskDefinitions.action'
					
				},
				//初始化页面元素
				initElement:function(doc){
					
				
					$edit_window = $('#roleEdit-win');
					$flowList = $(doc).find("#dg-flowList");
					
					$flowListGrid = $flowList.datagrid({
						height : _bizProto.gridHeight,
						url : baseLeftQueryUrl,					
						//onClickRow:function(rowIndex, rowData){private_method.getPermission(rowIndex, rowData)}
						onDblClickRow : function(rowIndex, rowData){private_method.getFlowConfig(rowIndex, rowData)}
					});
	
					$activityList = $(doc).find("#dg-activityList");
					
					
					var bizFlowListUrl = _bizProto.rootPath + '/permission/query/getFlowList.action'
					
					var activity_type_url = _bizProto.rootPath + '/dictionary/getTree.action?id=' + 115
					$activityListGrid=	$activityList.datagrid({
						url : baseRightByLeftIdQueryUrl,
						height : _bizProto.gridHeight,
						onClickCell:private_method.editCell,
						columns:[[
//							{field:'ck',checkbox:"true"},        
							{field:'activityId',title:'节点ID',halign:'center',width:30},        
							{field:'activityName',title:'节点名称',width:30,align:'center',halign:'center'},
							{field:'activityType',title:'节点类型',width:30,align:'center',halign:'center',
								editor:{type:'combobox',
									options:{
										valueField:'dic_value',
										textField:'name',										
										url:activity_type_url,										
										onSelect:function(record){
//											var _data =$($activityListGrid).datagrid('getData')
//											_data = _data.rows
//											$($activityListGrid).datagrid('getRows')[editIndex]['activityType'] = record.activityType;
//											
//											_data[editIndex].bizid=record.id
										}
									}
								}
							},
							{field:'sortedid',title:'排序编号',width:30,align:'center',halign:'center',editor:'text'},
							{field:'bizname',title:'业务名称',width:30,halign:'center',
								editor:{type:'combobox',
									options:{
										valueField:'name',
										textField:'name',										
										url:bizFlowListUrl,
										
										
										onSelect:function(record){
											var _data =$($activityListGrid).datagrid('getData')
											_data = _data.rows
											$($activityListGrid).datagrid('getRows')[editIndex]['bizname'] = record.name;
											$($activityListGrid).datagrid('getRows')[editIndex]['fullBizcode'] = record.fullBizcode;
											_data[editIndex].bizid=record.id
										}
									}
								}
							},        
							{field:'fullBizcode',title:'业务编码',width:30,align:'right',halign:'center'},
							{field:'activityConfig',title:'流程配置',width:100,align:'right',halign:'center',editor:'text'}
						]],
						rowStyler:function(index,row){   
					        if (row.state){
					        	
				        	 	if (row.state<0){
				        			return 'background-color:pink;';				        			
					        	}		        			
					        	 
					              
					        }   
					    },
					    rownumbers:true,
						animate: true,										
						striped:true,
						singleSelect:true,
						border:true,					
						idField: 'id',										
						fitColumns: true
					})	
					
					private_method.bindBtnEvent()
					// 搜索框
					$(doc).find("#searchbox").searchbox({
						menu : "#mm-rolePermission-role",
						prompt : '模糊查询',
						searcher : function(value, name) {
							var str = "{\"roleName\":\"" + name + "\",\"value\":\""
									+ value + "\"}";
							var obj = eval('(' + str + ')');
							$flowList.datagrid('reload', obj);
						}
	
					});
					
					$.messager.progress('close');
				},
				//根据左侧点击事件更新右侧流程节点列表信息
				getFlowConfig:function(rowIndex,rowData){
					if(rowData){
						if(rowData.id){
							$($activityListGrid).datagrid('reload',{procDefinitionId:rowData.id})
						}
						procDefinitionId = procDefinitionId
					}
					var _options = $($activityListGrid).datagrid('getColumnOption','activityName');
					
					
					
					_options.editor=
						{type:'combobox',
							options:{
								valueField:'name',
								textField:'name',										
								url:flowNodeUrl +'?processDefinitionId='+rowData.id,																	
								onSelect:private_method.changeActivityId
							}
						}
				},
				
				changeActivityId:function(record){
					var _data =$($activityListGrid).datagrid('getData')
					_data = _data.rows
					$($activityListGrid).datagrid('getRows')[editIndex]['activityId'] = record.id;											
					_data[editIndex].bizid=record.id
				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					
					var window_btn = $(_bizProto.pageDoc).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},
				//功能按钮监听函数
				function_btn_listenner:function(e){
					
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
					
				},
				
				//功能按钮监听函数
				save:function(){
					
					$($activityListGrid).datagrid('acceptChanges');
					var _data = $($activityListGrid).datagrid('getData')
					if(_data && _data.rows){
						//save data to remote
						
						var _url = _bizProto.rootPath + '/flowTaskConfig/save.action'
						$.ajax({
							url:_url,
							type : 'post',
							dataType:'json',
							data:$.toJSON(_data.rows),
							contentType:'appliction/json;charset=utf-8',						
							success:function(data){
								msgServ.Msgslide('操作成功');
							}
						})
					}					
				},
				add:function(){
					var record = $($activityListGrid).datagrid('getSelected');
					if(!record){
						var records = $($activityListGrid).datagrid('getRows');
						
						if(records && records.length>0){
							record = records[0]
						}
					}
					
					var _param={}
					if(record){
						_param={
							activityId:record.activityId,
							activityName:record.activityName,
							procDefinitionId:record.procDefinitionId
						}
					}
					$($activityListGrid).datagrid('appendRow',_param);
				},
				
				remove:function(){
					var record = $($activityListGrid).datagrid('getSelected');
					if(record){
						var rowindex =$($activityListGrid).datagrid('getRowIndex',record);
						
						$($activityListGrid).datagrid('deleteRow',rowindex);
					}else{
						msgServ.Msgslide('请选择删除的记录');
					}
				},
				
				reload:function(){
					$.ajax({
						url:_bizProto.rootPath +'/flowTaskConfig/query/reloadConfig.action',
						success:function(data){
							msgServ.Msgslide('刷新缓存成功');
						}
					})
				},
				
				editCell:function(index,field,value){
					if(window.console)
						console.info(' editCell fired ...')
					if(field=='activityName' ||field=='activityConfig' || field=='bizname' || field=='sortedid' || field=='activityType' ){
						if(editIndex>-1 && (editIndex!=index)){
							$($activityListGrid).datagrid('acceptChanges');
							$($activityListGrid).datagrid('endEdit',editIndex);							
						}
						
						$($activityListGrid).datagrid('beginEdit',index);
							editIndex = index
						
					}
				}
			}			
			

			var public_method = {
				init:function(doc){
					private_method.init(doc)
				}
				,
				openPage:function(){
					
				}
			};

			return public_method;

		});